//
//  ShareView.m
//  yunyan
//
//  Created by haichuang on 2018/3/27.
//  Copyright © 2018年 haichuang. All rights reserved.
//

#import "ShareView.h"
#import "OptionButton.h"
@interface ShareView()
@property(nonatomic,strong)UIView* buttonBgView;//
@property(nonatomic,strong)NSArray * optionButtonArray;//操作按钮数组
@end

@implementation ShareView

- (instancetype)init {
    self = [super init];
    
    if (self != nil) {
        [self initShareView];
        
    }
    
    return self;
}

-(void)initShareView{
    
  
    [self createView];
    
}

-(void)createView{
    
 
    
    //创建背景视图
    self.buttonBgView = [[UIView alloc]init];
    [self addSubview:self.buttonBgView];
    //关闭停靠模式
    self.buttonBgView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.buttonBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self);
        make.top.equalTo (self.mas_top).offset (40.0f);
    }];
    
    //创建更多选项按钮
    
    NSArray * optionButtonNames = @[@"新浪微博",@"微信好友",@"朋友圈",@"QQ好友"];
    
    NSArray * optionButtonImages = @[@"new-share_sina_default",@"new-share_WeChat_default",@"new-share_friends_default",@"new-share_qq_default"];
    
    
    
    //定义选项按钮的高度和宽度
    CGFloat optionButtonWidth = ScreenWidth/4;
    CGFloat optionButtonHeight = ScreenHeight/4;
    
    
    
    //定义存取选项按钮
    _optionButtons = [NSMutableArray array];
    [optionButtonNames enumerateObjectsUsingBlock:^(NSString *buttonName, NSUInteger idx, BOOL *stop) {
        //九宫格布局 取余 取整
        NSString * imageName = optionButtonImages[idx];
        
        OptionButton * opBtn1 = [[OptionButton alloc] initWithTitle:buttonName image:[UIImage imageNamed:imageName] frame:CGRectMake((idx % 4)*optionButtonWidth, (idx / 4) * optionButtonHeight , optionButtonWidth, optionButtonHeight)];
       
        opBtn1.layer.shadowOpacity = 0.;
        opBtn1.layer.shadowOffset = CGSizeMake(0, 0);
        [self.buttonBgView addSubview:opBtn1];
        
        [_optionButtons addObject:opBtn1];
        opBtn1.button.tag = idx +1000 ;
        
        [opBtn1.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        
    }];
    UIView * lineView = [[UIView alloc] init];
    [self addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo (_buttonBgView.mas_bottom).offset (-50.0f);
        make.left.right.equalTo (_buttonBgView);
        make.height.equalTo (@(1.0f));
    }];
   lineView.backgroundColor = RGBA(238, 238, 238, 1);
  
    UIButton * cancelBtn = [[UIButton alloc] init];
    [self addSubview:cancelBtn];
    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.bottom.right.equalTo (_buttonBgView);
        make.height.equalTo (@(50.0f));
      
    }];
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn  setTitleColor:RGBA(68, 68, 68, 1) forState:UIControlStateNormal];
    cancelBtn.titleLabel.font = [UIFont systemFontOfSize:15.0f];
    [cancelBtn addTarget:self action:@selector(cancelBtn) forControlEvents:UIControlEventTouchUpInside];
}

-(void)buttonAction:(UIButton *)sender{
    
    switch (sender.tag) {
        case 1000:{
            [self share:SSDKPlatformTypeSinaWeibo];
        }
            return;
            break;
        case 1001:{
          [self share:SSDKPlatformTypeWechat];
        }
            return;
            break;
        case 1002:{
         [self share:SSDKPlatformSubTypeWechatTimeline];
        }
            break;
        case 1003:
        {
             [self share:SSDKPlatformTypeQQ];
        }
            return;
            break;
            
        default:
            break;
    }
    
    
}

-(void)cancelBtn{
    if (_cancelBlock) {
        
        _cancelBlock();
        
    }
}

-(void)share:(SSDKPlatformType)type{

  NSString * imageUrl =   USER_DEFAULTGet(@"shareImage");
  NSString * titleStr = USER_DEFAULTGet(@"shareTitle");
  NSString * contentStr = USER_DEFAULTGet(@"shareContent");
  NSString * shareUrl = USER_DEFAULTGet(@"shareUrl");

            NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
            [shareParams SSDKSetupShareParamsByText:contentStr
                                             images:ImageUrl(imageUrl)
                                                url:[NSURL URLWithString:shareUrl]
                                              title:titleStr
                                               type:SSDKContentTypeAuto];
            [ShareSDK share:type //平台类型`
                 parameters:shareParams
             onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
                 switch (state) {
                     case 1:
                         [MBProgressHUD showMessag:@"分享成功"  withDelay:0 toView:self withMode:MBProgressHUDModeText hideWithDelay:1.5];
                         [self shareBackRequest];
                       
                         break;
                     case 2:
                         
                         [MBProgressHUD showMessag:@"分享失败"  withDelay:0 toView:self withMode:MBProgressHUDModeText hideWithDelay:1.5];
              
                         break;
                     case 3:
                         
                          [MBProgressHUD showMessag:@"取消分享"  withDelay:0 toView:self withMode:MBProgressHUDModeText hideWithDelay:1.5];
                         break;
                     default:
                         break;
                 }
             }];

}

-(void)shareBackRequest{
    
    NSDictionary * dic = @{@"user_id":USER_DEFAULTGet(@"user_id"),
                           @"token":USER_DEFAULTGet(@"token"),
                           @"mac_code":[SHJudgeTools getDeviceId],
                           @"type":USER_DEFAULTGet(@"shareType"),
                           @"news_id":USER_DEFAULTGet(@"shareNewsId")
                           };
    
    [PostNetWorking postNetWorkingStr:RequestUrlStr(share_back_api) andparameters:dic Success:^(id responseObject) {
        NSLog(@"统计成功");
        
    } failed:^(NSURLSessionDataTask * _Nullable task, NSError *error) {
        
    }];
}


@end

